package org.example.web.controller;

import org.example.web.domain.Emp;
import org.example.web.domain.QueryPage;
import org.example.web.domain.Result;
import org.example.web.service.EmpService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;

@RestController
@RequestMapping("/emps")
public class EmpController {
    @Autowired
    private EmpService empService;

    /**
     * 分页模糊查询所有员工信息
     * @param name
     * @param gender
     * @param begin
     * @param end
     * @param page
     * @param pageSize
     * @return
     */
    @GetMapping
    public Result pageQuery(String name,
                            Integer gender,
                            @DateTimeFormat(pattern = "yyyy-MM-dd") Date begin,
                            @DateTimeFormat(pattern = "yyyy-MM-dd") Date end,
                            int page,
                            int pageSize
                            ){
        QueryPage queryPage = empService.pageQuery(name,gender,begin,end,page,pageSize);
        return Result.success(queryPage);
    }

    /**
     * 批量删除员工
     * @param ids
     * @return
     */
    @DeleteMapping("/{ids}")
    public Result deleteById(@PathVariable("ids") List ids){
        empService.deleteById(ids);
        return Result.success();
    }

    /**
     * 新增员工
     * @param emp
     * @return
     */
    @PostMapping
    public Result add(@RequestBody Emp emp){
        empService.add(emp);
        return Result.success();
    }

    /**
     * 根据id查询指定员工
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    public Result getById(@PathVariable("id") Long id){
        return Result.success(empService.getById(id));
    }

    @PutMapping
    public Result update(@RequestBody Emp emp){
        empService.update(emp);
        return Result.success();
    }
}
